package grasil.org.prototyp;

import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import businesCard.BusinessCardAdapter;
import businesCard.BusinessCardDetail;
import businesCard.BusinessCardMainActivity;
import domain.BusinessCard;

/**
 * Created by Peter on 9. 8. 2014.
 */
public class BusinessCardFragment extends ListFragment implements AdapterView.OnItemClickListener {

    private static final String LOG = "BusinessCardFragment";
    private BusinessCardAdapter adapter;
    private List<BusinessCard> businessCards;

    public BusinessCardFragment() {
        Log.d(LOG, "BusinessCardFragment()");
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(LOG, "onCreate(Bundle savedInstanceState)");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_business_card, null, false);
        Log.d(LOG, "onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)");
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new BusinessCardAdapter(getActivity(), getBusinessCards());
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
        Log.d(LOG, "onActivityCreated(Bundle savedInstanceState)");
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(LOG, "onAttach(Activity activity)");
    }



    @Override
    public void onStart() {
        super.onStart();
        Log.d(LOG, "onStart()");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(LOG, "onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(LOG, " onPause()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(LOG, "onDestroy()");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(LOG, "onDetach() ");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(LOG, "onStop())");
    }


    private List<BusinessCard> getBusinessCards() {
        List<BusinessCard> businessCards = new ArrayList<BusinessCard>();
        BusinessCard b1 = new BusinessCard();
        b1.setName("Business Card 1");
        b1.setPriority(1);
        b1.setDateCreated(new Date());

        BusinessCard b2 = new BusinessCard();
        b2.setName("Business Card 2");
        b2.setPriority(2);
        b2.setDateCreated(new Date());

        BusinessCard b3 = new BusinessCard();
        b3.setName("Business Card 2");
        b3.setPriority(2);
        b3.setDateCreated(new Date());

        businessCards.add(b1);
        businessCards.add(b2);
        businessCards.add(b3);

        return businessCards;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d(LOG,"onItemClick(AdapterView<?> parent, View view, int position, long id)");
        Intent intent=new Intent(getActivity(), BusinessCardDetail.class);
        startActivity(intent);
    }
}
